package samples
{
	import flash.events.Event;
	import flash.events.MouseEvent;
	import marcel.controller.command.IAsyncCommand;
	import marcel.core.Plugin;
	import marcel.debug.*;
	import marcel.display.gui.buttons.SimpleTextButton;
	import marcel.display.View;
	import marcel.events.AsyncRequestEvent;


	public class FrontControllerSample extends View
	{
		public static const MY_EVENT_TYPE:String = "myEventType";
		
		public function FrontControllerSample()
		{
			// add command (can be done anywhere in app code)
			Plugin.plugin.frontController.addCommand(MY_EVENT_TYPE, new LoadImageCommand());
			
			// async command so listening for end of it
			var c : IAsyncCommand = Plugin.plugin.frontController.getCommand(MY_EVENT_TYPE) as IAsyncCommand;
			c.addEventListener(AsyncRequestEvent.RESULT, _onResult);
			
			// add button
			addChildWithProps(new SimpleTextButton("execute"), { x:200, y:200, click:_onExecute } );
		}
		
		private function _onResult(event : AsyncRequestEvent) : void
		{
			addChildWithProps(event.result, { x:400, y:200 } );
		}
		
		private function _onExecute(event : MouseEvent) : void
		{
			Plugin.plugin.dispatchEvent(new Event(MY_EVENT_TYPE));
		}
	}
}


import flash.events.Event;
import marcel.controller.command.AsyncCommand;
import marcel.core.Plugin;
import marcel.debug.*;
import marcel.events.FileLoaderEvent;
import marcel.external.FileLoader;
import marcel.external.FileType;

internal class LoadImageCommand extends AsyncCommand
{
	private var _imgLoader : FileLoader;

	public function LoadImageCommand()
	{
		super(1, 5);
		_imgLoader = new FileLoader(FileType.IMAGE);
		_imgLoader.addEventListener(FileLoaderEvent.COMPLETE, onImgLoaded);
	}
	
	override public function execute(event:Event = null):void
	{
		super.execute(event);
		debugi("Execute !");
		_imgLoader.load(Plugin.plugin.config.getVar('imagedir') + "tween_test.jpg");
	}
	
	override public function kill():void 
	{
		_imgLoader.removeEventListener(FileLoaderEvent.COMPLETE, onImgLoaded);
		_imgLoader.cancelLoad();
	}
	
	private function onImgLoaded(e:FileLoaderEvent):void 
	{
		notifyResult(e.content);
	}
}
